<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-10-17
 * Time: 上午10:57
 */
namespace Api\Common;

use Common\HttpStatus;
use Common\PlutoApi;
use Common\Request;
use Common\RequestApi;
use Common\Response;
use Common\ResponseApi;
use DB\VerifyCode;

class SendSmsRequest extends RequestApi{
  /**
   * @var int 联系方式
   */
  public $phone;
}

class SendSmsResponse extends ResponseApi{
  /**
   * @var string 提示语句
   */
  public $message;
}
class SendSms extends PlutoApi{
  protected function run(Request $request, Response $response) {
    /**
     * @var $rqData SendSmsRequest
     */
    $rqData = $request->data;
    if (empty($rqData->phone) || !is_int($rqData->phone)){
      $response->httpStatus = HttpStatus::PARAMETER_ERROR;
      $response->httpMessage = "phone is not set or is not int";
      return;
    }
    $verifyCode = new VerifyCode();
    $verifyCode->initVerifyCode($rqData->phone);
    $res = \Common\SendSms::sendSms($rqData->phone,$verifyCode->getVerifyCode());
    $response->data = new SendSmsResponse();
    if (empty($res->Code) || $res->Code != "OK"){
      $response->httpStatus = HttpStatus::ERROR;
      $response->httpMessage = "验证码发送失败";
      return;
    }
    $response->data->message = $res->Message;
  }
}